//
//  WBMainViewController.swift
//  仿写微博
//
//  Created by Apple on 17/6/30.
//  Copyright © 2017年 tuimao. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {
    //撰写按钮添加
    lazy var composeButton:UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置子控制器
        setupChildControllers()
        //设置撰写按钮
        setupcomposeButton()
    }
    /*
     portrait  竖屏
     landscape 横屏
     
     */
    // -使用代码控件设置设备的方向，好处，可以在需要横屏的时候，单独处理
    // -设置支持的方向后，当前的控制器及子控制器都会遵守这个方向！
    // -如果播放视频，通常是通过modal展现
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        
        return .portrait
    }
    
    //中间撰写微博的点击事件
    //FIXME:点击事件
    @objc fileprivate func composeStatus(){
        print("撰写微博")
        //FIXME:测试旋转屏幕
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.cz_random()
        let nav = UINavigationController(rootViewController: vc)
        present(nav, animated: true, completion: nil)
        
    }
    
}

//类扩展
extension WBMainViewController{
    //设置撰写按钮
    fileprivate func setupcomposeButton()  {
        tabBar.addSubview(composeButton)
        //计算按钮的宽度
        let count = CGFloat(childViewControllers.count)
        let w = tabBar.bounds.width / count - 1
        //CGRectInset正数向内缩进，负数向外
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        //撰写按钮点击事件
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
        
    }
     /// 设置子控制器
     fileprivate func setupChildControllers() {
        let array = [["clsName":"WBHomeViewController","title":"首页","image":"home"],["clsName":"WBMessageViewController","title":"消息","image":"discover"],
            ["clsName":"xx"],
            ["clsName":"WBDiscoverViewController","title":"发现","image":"home"],
            ["clsName":"WBProfileViewController","title":"我","image":"profile"]]
        var arrayM = [UIViewController]()
        for dic in array {
            arrayM.append(controller(dict: dic))
        }
        
        viewControllers = arrayM
     }
    
    
    /// 设置子控制器的title，image
    ///
    /// - Parameter dict: 字典
    /// - Returns: 一个控制器
    private func controller(dict:[String:String]) -> UIViewController {
        guard let clsName = dict["clsName"],
                  let title = dict["title"],
                  let image = dict["image"],
                  let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
        else {
                return UIViewController()
             }
        let vc = cls.init()
        vc.title = title
        //"tabbar_home_selected"
        vc.tabBarItem.image = UIImage(named:"tabbar_" + image)
        vc.tabBarItem.selectedImage = UIImage(named:"tabbar_" + image + "_selected")?.withRenderingMode(.alwaysOriginal)
        //设置tabBarTitle的字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: UIControlState(rawValue: 0))
        let nav = WBNavigationController(rootViewController: vc)
        return nav
    }
    
}
